<!---
@Author: Mark Solis
@Module: Leave Form Post Process
@Date: Nov. 22, 2012
--->
<cfparam name="GETRECORD.ROUTEID" default="01B89804-D361-71F0-0C9E00044ADA0547"> 
<CFTRY>
	<cfset ok=true>
    <CFSET LVHRS="">
    <CFSET ISWHLDY=0>
    <CFSET NOOFDYS=1>
    <CFSET LATEUNDRTIME = "NA">
    
    <CFQUERY NAME = "GETLVDTLS" DATASOURCE="#CLIENT.COMPANY_DSN#">
        SELECT *
        FROM CINLEAVEAPPSI
        WHERE ROUTEID ='#GETRECORD.ROUTEID#'
    </CFQUERY>
    <CFQUERY NAME = "GETWRKCODE" DATASOURCE="#CLIENT.COMPANY_DSN#">
        SELECT WORKINGDAYSCODE 
        FROM CMFPA
        WHERE PERSONNELIDNO ='#GETLVDTLS.PERSONNELIDNO#'
    </CFQUERY>
    <CFQUERY NAME = "GETWRKHR" DATASOURCE="#CLIENT.COMPANY_DSN#">
        SELECT WORKINGHOURSFORLV
        FROM CLKWORKINGDAYS
        WHERE WORKINGDAYSCODE ='#GETWRKCODE.WORKINGDAYSCODE#'
    </CFQUERY>

    <cfset wrkhr = 8>
    <cfif GETWRKHR.WORKINGHOURSFORLV neq "">
        <cfset wrkhr = #GETWRKHR.WORKINGHOURSFORLV#>
    </cfif>


    <CFSET LVHRS = DateDiff("h", GETLVDTLS.STARTINGTIME, GETLVDTLS.ENDINGTIME)>

    <CFSET TEMP = #wrkhr# / 2>
    <CFSET ISWHLDY = #LVHRS# / 2>


    <CFIF ISWHLDY GT TEMP>
        <CFSET ISWHLDY = 1>
        <CFSET LVHRS = #wrkhr#>
    <CFELSE>
        <CFSET ISWHLDY = 0>
        <CFSET LVHRS = #temp#>
        <CFSET LATEUNDRTIME = #GETLVDTLS.LATEUNDERTIME#>
    </CFIF>

    <CFSET NOOFDYS = DateDiff("d", GETLVDTLS.STARTINGDATE, GETLVDTLS.ENDINGDATE)>
    <CFSET NOOFDYS = #NOOFDYS# +1>
    
	<CFTRANSACTION action="begin">
    	<cftry>
            <CFQUERY NAME = "INSERTM" DATASOURCE="#CLIENT.COMPANY_DSN#">
                INSERT INTO CINLEAVEAPPSM (
                PERSONNELIDNO,
                STARTINGDATE,
                ENDINGDATE,
                LEAVETYPE,
                APPROVED,
                LEAVEHOURS,
                WHOLEDAY,
                NOOFDAYS,
                ROUTED,
                LATEUNDERTIME,
                DATEACTIONWASDONE,
                RECCREATEDBY,
                RECDATECREATED,
                USERID,
                DATELASTUPDATE,
                TIMELASTUPDATE,	
                APPLICATIONDATE,
                WITHPAY
                )
                VALUES (
                    '#GETLVDTLS.PERSONNELIDNO#',
                    #CreateODBCDate(GETLVDTLS.STARTINGDATE)#,
                    #CreateODBCDate(GETLVDTLS.ENDINGDATE)#,
                    '#GETLVDTLS.LEAVETYPE#',
                    'Y',
                    '#LVHRS#',
                    '#ISWHLDY#',
                    '#NOOFDYS#',
                    'Y',
                    '#LATEUNDRTIME#',
                    #CreateODBCDate(Now())#, 
                    '#GETLVDTLS.RECCREATEDBY#',
                    #CreateODBCDate(GETLVDTLS.RECDATECREATED)#,
                    '#client.userid#',
                    #CreateODBCDate(Now())#,
                    '#timeformat(Now(),"HH:mm:ss")#',
                    '#GETLVDTLS.APPLICATIONDATE#',
                    '#GETLVDTLS.WITHPAY#'
                )
            </CFQUERY>
            
            <CFSET NOOFDYS = #NOOFDYS# -1>
            <CFLOOP INDEX = "i" to = "#NOOFDYS#" from ="0" >
                <CFQUERY NAME = "INSERTD" DATASOURCE="#CLIENT.COMPANY_DSN#">
                    INSERT INTO CINLEAVEAPPSD (PERSONNELIDNO, REFERENCEDATE, APPROVED, LEAVEHOURS, WITHPAY,LEAVETYPE
                    ,RECCREATEDBY,RECDATECREATED,USERID,DATELASTUPDATE,TIMELASTUPDATE,LATEUNDERTIME) 
                    VALUES(
                        '#GETLVDTLS.PERSONNELIDNO#',
                        #CreateODBCDate(DateAdd("d",i,GETLVDTLS.STARTINGDATE))#,
                        'Y',
                        '#LVHRS#',
                        '#GETLVDTLS.WITHPAY#',
                        '#GETLVDTLS.LEAVETYPE#',
                        '#GETLVDTLS.RECCREATEDBY#',
                        '#GETLVDTLS.RECDATECREATED#',
                        '#client.userid#',
                        #CreateODBCDate(Now())#,
                        '#timeformat(Now(),"HH:mm:ss")#',
                        '#LATEUNDRTIME#'
                    )
                </CFQUERY>
            </CFLOOP>
            <cfcatch>
                <cfset ok = false>
              
            </cfcatch>
        </cftry>
        
        
        <cfif ok>
        	<cftransaction action="commit">
            <CFSET sMessPostApproval = "Leave Succesfully Updated.">
        <cfelse>
        	<cftransaction action="rollback">
        </cfif>
        
    </CFTRANSACTION>
    <CFCATCH>
        <!---CFSET sMessPostApproval = "An error occured please contact your iBOS/e administrator."--->
          <CFSET sMessPostApproval = #cfcatch.Detail#>
    </CFCATCH>	
</CFTRY> 
<cfoutput>#sMessPostApproval#</cfoutput>




















